// RedJey Graphics User Interface Library for J2ME Platform.
// Copyright (C) 2008 Yevgen Krapiva
//
// This library is free software; you can redistribute
// it and/or modify it under the terms of the
// GNU Lesser General Public License as published
// by the Free Software Foundation; either version 3.0
// of the License, or any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

package com.incrediblesoft.redjey.font4me;

import javax.swing.*;
import java.awt.*;

/**
 * @author Yevgen Krapiva<br>
 *         Homepage: http://code.google.com/p/redjey/<br>
 *         Feedback: ykrapiva@gmail.com<br>
 *         Copyright (C) 2008 Yevgen Krapiva<br>
 */


public class FontPreviewCanvas extends JPanel
{
    private int canvasWidth, canvasHeight;
    private String text;
    private String[] splittedText;
    private CustomFont customFont;

    private Font font;

    private String charmap;
    private Color foregroundColor, backgroundColor;
    private boolean fontCaseSensitive;

    public void updateCustomFont()
    {
        Graphics g = getGraphics();

        if (g != null && charmap != null && charmap.length() > 0)
        {
            customFont = new CustomFont(g, font, charmap, foregroundColor, backgroundColor, fontCaseSensitive);

            repaint();
        }
    }

    public void paintComponent(Graphics gr)
    {
        super.paintComponent(gr);


        gr.translate((getWidth() - canvasWidth) / 2, (getHeight() - canvasHeight) / 2);
        Rectangle clipRect = gr.getClipBounds();

        gr.setColor(Color.black);
        gr.drawRect(0, 0, canvasWidth-1, canvasHeight-1);

        gr.clipRect(0, 0, canvasWidth, canvasHeight);

        if (customFont != null)
        {
            splittedText = CustomFont.splitString(text,  customFont, canvasWidth, 0);
            for (int y=0; y<splittedText.length; y++)
                customFont.drawString(gr, splittedText[y], 0, y * customFont.getHeight());
        }

        gr.setClip(clipRect.x, clipRect.y, clipRect.width, clipRect.height);
        gr.translate( - (getWidth() - canvasWidth) / 2, - (getHeight() - canvasHeight) / 2);
    }

    public int getCanvasWidth()
    {
        return canvasWidth;
    }

    public void setCanvasWidth(int canvasWidth)
    {
        this.canvasWidth = canvasWidth;
        repaint();
    }

    public int getCanvasHeight()
    {
        return canvasHeight;
    }

    public void setCanvasHeight(int canvasHeight)
    {
        this.canvasHeight = canvasHeight;
        repaint();
    }

    public CustomFont getCustomFont()
    {
        return customFont;
    }

    public String getText()
    {
        return text;
    }

    public void setText(String text)
    {
        this.text = text;
        repaint();
    }

    public Font getFont()
    {
        return font;
    }

    public void setFont(Font font)
    {
        this.font = font;
        updateCustomFont();
    }

    public String getCharmap()
    {
        return charmap;
    }

    public void setCharmap(String charmap)
    {
        this.charmap = charmap;
        updateCustomFont();
    }

    public Color getForegroundColor()
    {
        return foregroundColor;
    }

    public void setForegroundColor(Color foregroundColor)
    {
        this.foregroundColor = foregroundColor;
        updateCustomFont();
    }

    public Color getBackgroundColor()
    {
        return backgroundColor;
    }

    public void setBackgroundColor(Color backgroundColor)
    {
        this.backgroundColor = backgroundColor;
        updateCustomFont();
    }

    public boolean isFontCaseSensitive()
    {
        return fontCaseSensitive;
    }

    public void setFontCaseSensitive(boolean fontCaseSensitive)
    {
        this.fontCaseSensitive = fontCaseSensitive;
        updateCustomFont();
    }

    public char getCharAt(int x, int y)
    {
        if (splittedText == null) return 0;

        x -= (getWidth() - canvasWidth) / 2;
        int line_y = y / customFont.getHeight();
        if (line_y < 0 || line_y >= splittedText.length)
            return 0;

        String line = splittedText[line_y];

        int posX = 0;

        for(int i=0; i<line.length(); i++)
        {
            char ch = line.charAt(i);
            if (x >= posX && x < posX + customFont.charWidth(ch))
                return ch;
            posX += customFont.charWidth(ch);
        }

        return 0;
    }

}

